package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Bed;
import com.neuedu.yyzx.service.BedService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 金山
 * 项目：yyzx
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/25 14:23
 */
@RestController
@RequestMapping("/bed")
@Tag(name = "床位管理")
public class BedController {

    @Autowired
    private BedService bedService;

    /**
     * http://localhost:8080/bed/list
     *
     * @return
     */
    @GetMapping("/list")
    public Ret list() {
        return Ret.ok(bedService.list());
    }

    @GetMapping("/page")
    public Ret<IPage<Bed>> page(@RequestParam(defaultValue = "1") int current,
                                @RequestParam(defaultValue = "10") int size,
                                Bed bed) {
        Page<Bed> page = new Page<>(current, size);
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();

        // 使用实体类的属性名
        if (bed.getBedNo() != null && !"".equals(bed.getBedNo().trim())) {
            queryWrapper.like(Bed::getBedNo, bed.getBedNo());
        }
        if (bed.getRoomNo() != null && !"".equals(bed.getRoomNo().trim())) {
            queryWrapper.like(Bed::getRoomNo, bed.getRoomNo());
        }

        // 根据 remarks 筛选正在使用（1）或使用历史（2）
        if (bed.getRemarks() != null) {
            queryWrapper.eq(Bed::getRemarks, bed.getRemarks());
        }

        // 只查询未删除的记录
        queryWrapper.eq(Bed::getIsDelete, "0");

        // ORDER BY createtime DESC
        queryWrapper.orderByDesc(Bed::getCreateTime);

        System.out.println("查询参数: " + bed);
        System.out.println("执行的SQL: " + queryWrapper.getSqlSegment());

        page = bedService.page(page, queryWrapper);
        return Ret.ok(page);
    }

    /**
     * 床位的添加或者更新
     *
     * @param bed
     * @return
     */
    @Operation(summary = "床位保存或者更新")
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Bed bed) {
        boolean success = bedService.saveOrUpdate(bed);
        return Ret.ok(success);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Ret<Bed> get(@PathVariable int id) {
        Bed bed = bedService.getById(id);
        return Ret.ok(bed);
    }

    /**
     * 根据主键删除
     *
     * /bed/{id}
     * method delete
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(bedService.removeById(id));
    }
}